﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.RecruitmentManagement;
using WaterCloud.Domain.ContractManagement;
using WaterCloud.Domain.SystemOrganize;

namespace WaterCloud.Service.RecruitmentManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-04-02 13:16
    /// 描 述：招聘计划服务类
    /// </summary>
    public class RecruitmentplanService : BaseService<RecruitmentplanEntity>, IDenpendency
    {
        public RecruitmentplanService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<RecruitmentplanEntity>> GetList(string keyword = "")
        {
           
            var data = repository.Db.Queryable<RecruitmentplanEntity, RoleEntity>((a, b) => new JoinQueryInfos(

            JoinType.Left, a.F_DutyId == b.F_Id

            ))
            .Select((a, b) => new RecruitmentplanEntity
            {
                F_Id = a.F_Id.SelectAll(),

                F_DutyName = b.F_FullName,


            }).MergeTable();




            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<RecruitmentplanEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
             //权限过滤
             query = GetDataPrivilege("a", "", query);
             return await query.OrderBy(a => a.F_Id , OrderByType.Desc).ToListAsync();
        }

        public async Task<List<RecruitmentplanEntity>> GetLookList(SoulPage<RecruitmentplanEntity> pagination,string keyword = "",string id="")
        {
           

            var query = repository.Db.Queryable<RecruitmentplanEntity, SystemSetEntity,OrganizeEntity, RoleEntity>((a, b,c,d) => new JoinQueryInfos(
                JoinType.Left, a.F_CompanyId == b.F_Id,
                JoinType.Left, a.F_OrganizeId == c.F_Id,
                JoinType.Left, a.F_DutyId == d.F_Id
                ))
                .Select((a, b,c,d) => new RecruitmentplanEntity
                {
                    F_Id = a.F_Id.SelectAll(),
                    F_CompanyName = b.F_CompanyName,
                    F_OrganizeName = c.F_FullName,
                    F_DutyName=d.F_FullName,
                    F_DeliveryCount= SqlFunc.Subqueryable<PersonnelEntity>().Where(b => b.F_PlanId == a.F_Id && b.F_State==0).Count(),
                    F_InterviewCount= SqlFunc.Subqueryable<PersonnelEntity>().Where(b => b.F_PlanId == a.F_Id && b.F_State==1).Count(),
                    F_AdmissionCount = SqlFunc.Subqueryable<PersonnelEntity>().Where(b => b.F_PlanId == a.F_Id && b.F_State == 2).Count(),


                }).MergeTable();

            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_EnCode.Contains(keyword)
                || a.F_FullName.Contains(keyword));
            }
            if(!string.IsNullOrEmpty(id))
            {
                query= query.Where(a=>a.F_Id==id);
            }
            //权限过滤
            query = GetDataPrivilege("a","",query);
            return  await query.ToPageListAsync(pagination);
        }

        public async Task<RecruitmentplanEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return data;
        }
        #endregion

        public async Task<RecruitmentplanEntity> GetLookForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            string[] temp;
            if (data.F_OrganizeId != null)
            {
                temp = data.F_OrganizeId.Split(',');
                data.F_OrganizeName = string.Join(",", repository.Db.Queryable<OrganizeEntity>().Where(a => temp.Contains(a.F_Id)).Select(a => a.F_FullName).ToList().ToArray());
            }

            return GetFieldsFilterData(data);
        }

        #region 提交数据
        public async Task SubmitForm(RecruitmentplanEntity entity, string keyValue)
        {
            if(string.IsNullOrEmpty(keyValue))
            {
                    //初始值添加
                entity.F_DeleteMark = false;
                entity.F_EnabledMark= false;
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                    //此处需修改
                entity.Modify(keyValue); 
                await repository.Update(entity);
            }
        }

        public async Task DeleteForm(string keyValue)
        {
            var ids = keyValue.Split(',');
            await repository.Delete(a => ids.Contains(a.F_Id.ToString()));
        }
        public async Task CloseForm(string keyValue, bool b)
        {
            var ids = keyValue.Split(',');
            if (b)
            {
                await repository.Update(a => ids.Contains(a.F_Id), a => new RecruitmentplanEntity
                {
                    F_EnabledMark = b,
                    F_EnabledTime = DateTime.Now,
                    F_EnabledUserId=currentuser.UserId,
                    F_EnabledUser=currentuser.UserName,

                });

            }
            else
            {
                await repository.Update(a => ids.Contains(a.F_Id), a => new RecruitmentplanEntity
                {
                    F_EnabledMark = b,
                    F_EnabledTime = null,
                    F_EnabledUserId = null,
                    F_EnabledUser = null,

                });

            }
          
        }
        public async Task ReviewForm(string keyValue, bool b)
        {
            var ids = keyValue.Split(',');
            if (b)
            {
                await repository.Update(a => ids.Contains(a.F_Id), a => new RecruitmentplanEntity
                {
                    F_ReviewMark= b,
                    F_ReviewTime = DateTime.Now,
                    F_ReviewUserId = currentuser.UserId,
                    F_ReviewUser = currentuser.UserName,

                });

            }
            else
            {
                await repository.Update(a => ids.Contains(a.F_Id), a => new RecruitmentplanEntity
                {
                    F_ReviewMark = b,
                    F_ReviewTime = null,
                    F_ReviewUserId = null,
                    F_ReviewUser = null,

                });

            }
        }
        #endregion

    }
}
